package lambda;

import org.junit.Test;

import java.util.Comparator;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author mubei
 * @date 2021/11/20
 * @description 1.举例  (o1,02)->Integer.compare(o1,o2)
 * 2.格式:
 * ->   lambda 操作符 或 箭头操作符
 * ->   左边是lambda形参
 * ->   右边是lambda方法题
 * <p>
 * 3.如何写:  六种情况
 * 1.无参 无返回值
 * <p>
 * 4. Lambda表达式的本质:作为接口的实例
 */
public class Test02 {


    //  语法格式1  无参无返回值
    @Test()
    public void test1() {
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("创建");
            }
        };
        r1.run();

        Runnable r2 = () -> System.out.println("真不错");
        r2.run();
    }


    //语法格式二  一个参数  无返回值
    @Test()
    public void test2() {
        Consumer con = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con.accept("嗯可以的");

        Consumer<String> con1 = (s) -> System.out.println(s);
        con1.accept("ah真漂亮");

        Consumer<String> con2 = s -> System.out.println(s);
        con1.accept("谁家的姑娘");
    }

    // 两个参数并且有返回值
    @Test()
    public void test3() {
        Comparator<Integer> com1 = new Comparator<Integer>(){
            @Override
            public int compare(Integer o1,Integer o2){
                return Integer.compare(o1,o2);
            }
        };
        System.out.println(com1.compare(10, 21));

        Comparator<Integer> com2 = (o1,o2)->Integer.compare(o1,o2);
        System.out.println(com2.compare(21, 10));
    }

    @Test()
    public void test5() {
        Runnable r1 = ()-> System.out.println("sss");
        r1.run();
    }


}
